package com.barbeque.mall.service.mallmanagement;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.order.*;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.user.MarketUser;
import com.barbeque.mall.mapper.MarketOrderGoodsMapper;
import com.barbeque.mall.mapper.MarketOrderMapper;
import com.barbeque.mall.mapper.MarketUserMapper;
import com.barbeque.mall.util.OrderConstant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author ycc
 * @date 2022/07/16 14:20:40
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    MarketOrderGoodsMapper goodsMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    //显示快递公司的选项
    @Override
    public List<ChannelVo> channel() {
        return orderMapper.selectCompanies();
    }

    //根据条件显示订单
    @Override
    public CommonData<MarketOrder> list(MarketOrder marketOrder, BasePageInfo pageInfo, Date start, Date end, Short[] orderStatusArray) {
        //1.获取查询所需的参数
        String sort = pageInfo.getSort();
        String order = pageInfo.getOrder();
        Integer id = marketOrder.getId();
        Integer userId = marketOrder.getUserId();
        String orderSn = marketOrder.getOrderSn();

        //2.开启分页
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());

        //3.拼接orderClause语句
        MarketOrderExample example = new MarketOrderExample();
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            //order by add_time desc
            example.setOrderByClause(sort+" "+order);
        }

        //4..拼接select语句
        MarketOrderExample.Criteria criteria = example.createCriteria();

        if (id!=null){
            criteria.andIdEqualTo(id);
        }
        if (userId!=null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(orderSn)) {
            orderSn ="%" + orderSn + "%";
            criteria.andOrderSnLike(orderSn);
        }
        if (start!=null) {
            criteria.andAddTimeGreaterThanOrEqualTo(start);
        }
        if (end!=null) {
            criteria.andAddTimeLessThanOrEqualTo(end);
        }
        if (orderStatusArray!=null && orderStatusArray.length!=0) {
            List<Short> shorts = new LinkedList<>();
            Collections.addAll(shorts, orderStatusArray);
            criteria.andOrderStatusIn(shorts);
        }
        //只显示未被删除的订单
        criteria.andDeletedEqualTo(false);

        //5.获取订单数据
        List<MarketOrder> orders = orderMapper.selectByExample(example);

        PageInfo<MarketOrder> info = new PageInfo<>(orders);

        //6.分页
        CommonData<MarketOrder> data = CommonData.data(info);

        return data;
    }

    //订单详情
    @Override
    public DetailVo detail(Integer id) {
        DetailVo detailVo = new DetailVo();

        MarketOrder order = orderMapper.selectByPrimaryKey(id);
        MarketUser user = userMapper.selectByPrimaryKey(order.getUserId());

        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        if (id!=null) {
            criteria.andOrderIdEqualTo(id);
        }
        List<MarketOrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);


        detailVo.setOrder(order);
        detailVo.setUser(user);
        detailVo.setOrderGoods(orderGoods);

        return detailVo;
    }

    //发货
    @Override
    public int ship(Map map) {
        MarketOrder order = new MarketOrder();
        //快递公司
        order.setShipChannel((String) map.get("shipChannel"));
        //快递编号
        order.setShipSn((String) map.get("shipSn"));
        //订单状态
        order.setOrderStatus(OrderConstant.SHIP_CODE);
        //修改发货时间
        order.setShipTime(new Date());

        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();

        //select语句
        criteria.andIdEqualTo((Integer) map.get("orderId"));

        return orderMapper.updateByExampleSelective(order,orderExample);
    }

    //退款
    @Override
    public int refund(Map map) {
        MarketOrder order = new MarketOrder();
        //订单状态
        order.setOrderStatus(OrderConstant.REFUND_CODE);
        //退款金额

        order.setRefundAmount(new BigDecimal((int) map.get("refundMoney")));
        //退款方式
        order.setRefundType(OrderConstant.REFUND_TYPE);
        //修改退款时间
        order.setRefundTime(new Date());

        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        //select语句，根据订单id退款
        criteria.andIdEqualTo((Integer) map.get("orderId"));

        return orderMapper.updateByExampleSelective(order,orderExample);
    }

    //删除订单
    @Override
    public int delete(int orderId) {
        MarketOrderExample keywordExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = keywordExample.createCriteria();
        //拼接select语句,根据订单id删除订单
        criteria.andIdEqualTo(orderId);
        //只能删除“已付款/已退款/系统取消/用户取消/用户收货/系统收货”
        LinkedList<Short> deleteList = new LinkedList<>();
        deleteList.add(OrderConstant.UNPAID_CODE);
        deleteList.add(OrderConstant.REQUESTREFUND_CODE);
        deleteList.add(OrderConstant.SHIP_CODE);
        criteria.andOrderStatusNotIn(deleteList);

        MarketOrder order = new MarketOrder();
        //修改删除状态码
        order.setDeleted(true);

        return orderMapper.updateByExampleSelective(order, keywordExample);
    }

    /**
     * 根据id查询订单详情，用于日志记录
 * @param id
     * @return com.barbeque.mall.bean.order.MarketOrder
     * @author 苏东宇
     * @since 2022/07/18 16:29
     */
    @Override
    public MarketOrder getOrderById(int id) {

        MarketOrder marketOrder = orderMapper.selectByPrimaryKey(id);


        return marketOrder;
    }
}
